//
//  CLReadViewStatusBottomView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/8/11.
//

#import "CLReadViewStatusBottomView.h"

@implementation CLReadViewStatusBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    
    // 电池
    CLBatteryView *batteryView = [[CLBatteryView alloc]init];
    self.batteryView = batteryView;
    batteryView.tintColor = [CLReadConfigure sharedInstance].statusTextColor;
    [self addSubview:batteryView];
    
    // 时间
    UILabel *timeLabel = [[UILabel alloc]init];
    self.timeLabel = timeLabel;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = CL_FONT_SA_10;
    timeLabel.textColor = [CLReadConfigure sharedInstance].statusTextColor;
    [self addSubview:timeLabel];
    
    // 进度
    UILabel *progress = [[UILabel alloc]init];
    self.progress = progress;
    progress.font = CL_FONT_SA_10;
    progress.textColor = [CLReadConfigure sharedInstance].statusTextColor;
    [self addSubview:progress];
    
    // 初始化调用
    [self didChangeTime];
    
    // 添加定时器
    [self addTimer];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect frame = self.frame;
    CGFloat w = frame.size.width;
    CGFloat h = frame.size.height;

    // 电池
    CGRect bf = self.batteryView.frame;
    bf.origin = CGPointMake(w - CLBatterySize().width, (h - CLBatterySize().height) / 2);
    self.batteryView.frame = bf;
    // 时间
    self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.batteryView.frame) - CL_SPACE_SA_50, 0, CL_SPACE_SA_50, h);
    
    // 进度
    self.progress.frame = CGRectMake(0, 0, CL_SPACE_SA_50, h);
}

- (void)didChangeTime {
    self.timeLabel.text = [NSDate timerStringFormat:@"HH:mm"];
    
    self.batteryView.batteryLevel = [UIDevice currentDevice].batteryLevel;
}

- (void)addTimer {
    if (_timer == nil) {
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(didChangeTime) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
}

- (void)removeTimer {
    if (_timer != nil) {
        
        [_timer invalidate];
        
        _timer = nil;
    }
}

@end
